package com.yanqu.road.server.cmd.kuang.cross;

import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.kuang.Kuang;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

@Cmd(code = Protocol.S_KUANG_AUTO_SEND_RANK_REWARD_FROM_CROSS, desc = "跨服发奖励来")
public class KuangAutoSendRankRewardFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        Kuang.CrossKuangAutoSendRankRewardMsg respMsg = Kuang.CrossKuangAutoSendRankRewardMsg.parseFrom(yanQuMessage.getBytes());
        List<Kuang.CrossKuangAutoSendRankUserTemp> userList = respMsg.getUserList();
        for (Kuang.CrossKuangAutoSendRankUserTemp userTemp : userList) {
            long serverId = userTemp.getServerId();
            if (serverId != GameServer.getInstance().getServerId()) {
                continue;
            }
            long userId = userTemp.getUserId();
//            int rank = userTemp.getRank();
            String reward = userTemp.getReward();
            // 奖励为空说明该名次没配奖励，则不发奖
            if (StringUtils.isNullOrEmpty(reward)) {
                continue;
            }
            // 发邮件
            String language = UserMgr.getLanguage(userId);
            String mailTitle = MultipleLanguageMgr.getContent(MailManager.KUANG_RANK_REWARD_TITLE, language);
            String mailContent = MultipleLanguageMgr.getContent(MailManager.KUANG_RANK_REWARD_CONTENT, language);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle);
        }
    }
}
